//
//  NetCar.m
//  CarCare
//
//  Created by ileo on 14-9-19.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "NetCar.h"
#import "AIUserInfo.h"
#import "NetEngineServer.h"

@implementation NetCar

+(void)addCar:(NSDictionary *)car success:(void (^)())success{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:car];
    [dic setObject:@(USERINFO.vehicleId) forKey:@"vehicleId"];
    [dic setObject:USERINFO.pastDevice forKey:@"imei"];

    [[[__SELF request:@"/violationNew/saveCar" withParams:dic type:REQUEST_GET] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips|RequestLoadShowSuccessTips] requestSuccess:^(id JSON) {
        if (success) success();
    }];
}

+(void)updateCar:(NSDictionary *)car success:(void (^)())success{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:car];
    [dic setObject:@(USERINFO.vehicleId) forKey:@"vehicleId"];
    [dic setObject:USERINFO.pastDevice forKey:@"imei"];
    if (![dic[@"isChangeCarStyle"] boolValue]) {
        [dic removeObjectForKey:@"carTypeId"];
    }
    [[[__SELF request:@"/violationNew/updCar" withParams:dic type:REQUEST_GET] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips|RequestLoadShowSuccessTips] requestSuccess:^(id JSON) {
        if (success) success();
    }];
}

+(void)delCar:(NSInteger)carID success:(void (^)())success{
    [[[__SELF request:@"/violationNew/delCar" withParams:@{@"carid":@(carID)} type:REQUEST_GET] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips|RequestLoadShowSuccessTips] requestSuccess:^(id JSON) {
        if (success) success();
    }];
}

+(void)bindCar:(NSInteger)carID styleId:(NSInteger)styleId success:(void (^)())success{
    [[[__SELF request:@"/violationNew/setBindCar" withParams:@{@"carid":@(carID),@"styleId":@(styleId),@"vehicleId":@(USERINFO.vehicleId),@"imei":USERINFO.pastDevice} type:REQUEST_GET] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips|RequestLoadShowSuccessTips] requestSuccess:^(id JSON) {
        if (success) success();
    }];
}

+(void)carListSuccess:(void (^)(NSArray<CarModel *> *))success fail:(void (^)())fail{
    [[__SELF request:@"/violationNew/getCarList" withParams:nil type:REQUEST_POST] requestSuccess:^(id JSON) {
        NSArray *cars = JSON[@"data"];
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:cars.count];
        [cars enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CarModel *model = [[CarModel alloc] initWithDic:obj];
            [models addObject:model];
        }];
        if (success) success(models);
    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
}

+(void)carImgWithStyleID:(NSInteger)styleID success:(void (^)(NSString *))success{
    [[[__SELF request:@"/violationNew/getCarStyle" withParams:@{@"styleid" : @(styleID)} type:REQUEST_POST] requestWithLoad:RequestLoadShowLoading] requestSuccess:^(id JSON) {
        if (success) success(JSON[@"data"][@"modelImg"]);
    }];
}


#pragma mark - old


+(NetCar *)locationWithImei:(NSString *)imei{
    return [__SELF request:@"/location/getrecentlocation" withParams:@{@"imei":imei} type:REQUEST_POST];
}

+(NetCar *)setOilNum:(NSString *)oilNum oilCity:(NSString *)oilCity{
    return [__SELF request:@"/Oil/setOil" withParams:@{@"imei":USERINFO.pastDevice, @"oil":oilNum, @"city":oilCity} type:REQUEST_POST];
}

+(NetCar *)setCarCarNo:(NSString *)carNo{
    return [__SELF request:@"/AutoInfo/upload" withParams:@{@"carno":carNo, @"engineno":USERINFO.engineNO,@"classno":USERINFO.vinNO} type:REQUEST_POST];
}

+(NetCar *)setCarEngineNo:(NSString *)engineNo{
    return [__SELF request:@"/AutoInfo/upload" withParams:@{@"engineno":engineNo, @"carno":USERINFO.carNo,@"classno":USERINFO.vinNO} type:REQUEST_POST];
}

+(NetCar *)setCarVIN:(NSString *)vin{
    return [__SELF request:@"/AutoInfo/upload" withParams:@{@"classno":vin, @"carno":USERINFO.carNo,@"engineno":USERINFO.engineNO} type:REQUEST_POST];
}


+(NetCar *)getCarBrandWithDrive:(NSString *)drive{
    return [__SELF request:@"/autoType/getAutoBrand" withParams:@{@"imei":drive} type:REQUEST_POST];
}

+(NetCar *)getCarModelWithDrive:(NSString *)drive brandID:(NSInteger)brandID{
    return [__SELF request:@"/autoType/getAutoModel" withParams:@{@"imei":drive, @"brandId":@(brandID)} type:REQUEST_POST];
}

+(NetCar *)getCarStyleWithDrive:(NSString *)drive modelID:(NSInteger)modelID{
    return [__SELF request:@"/autoType/getAutoStyle" withParams:@{@"imei":drive, @"modelId":@(modelID)} type:REQUEST_POST];
}

+(NetCar *)bindingDevice:(NSString *)device deviceCode:(NSString *)deviceCode{
    return [__SELF request:@"/autoType/setAutoBing" withParams:@{@"imei":device, @"code":deviceCode} type:REQUEST_POST];
}

+(NetCar *)bindingDevice:(NSString *)device CarStyle:(NSInteger)styleId vehicleId:(NSInteger)vehicleId{
    return [__SELF request:@"/autoType/setAutoStyle" withParams:@{@"imei":device,@"styleId":@(styleId), @"vehicleId":@(vehicleId)} type:REQUEST_POST];
}

//+(NetCar *)unbindingDevice{
//    return [__SELF request:@"/Bing/unBing" withParams:nil type:REQUEST_POST];
//}

+ (NetCar *)getBindingDeviceState
{
    return [__SELF request:@"/condition/status" withParams:@{@"vehicleData":USERINFO.pastDevice} type:REQUEST_POST];
}
+ (NetCar *)getCarSituation
{
    return [__SELF request:@"/condition/query" withParams:@{@"vehicleData":USERINFO.pastDevice} type:REQUEST_POST];
}

-(NSDictionary *)requestCommonParams{
    if (![USERINFO.session isNotNil]) {
        return nil;
    }
    return @{@"userid":@(USERINFO.userID),@"session":USERINFO.session};
}


@end
